#!/bin/env python
#_*_coding:utf-8_*_
'''
Created on Sep 22, 2015

@author: root
'''
import time
descriptors=list()
import sys
sys.path.append('./gen-py')
from config import ConfigService
from online import OnlineService
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
def master():

    try:
        dict={}
        transport = TSocket.TSocket('127.0.0.1', 8999)
        transport = TTransport.TFramedTransport(transport)
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
        client = ConfigService.Client(protocol)
        transport.open()
        msg=client.queryService("onlineServer","1.0")
        if len(msg)!=0:
            for i in msg:
                dict["ip"]=i.ip
                dict["port"] =i.port
        return dict

        transport.close()

    except Thrift.TException, ex:
        print "%s" % (ex.message)
def onlineinfo():
    try:
        date=master()
        transport = TSocket.TSocket(date["ip"], date["port"])
        transport = TTransport.TFramedTransport(transport)
        protocol = TBinaryProtocol.TBinaryProtocol(transport)
        client = OnlineService.Client(protocol)
        transport.open()
        msg=client.getOnlineNumInfo()
        return msg

        transport.close()

    except Thrift.TException, ex:
        print "%s" % (ex.message)

def pc(name):
    '''
    具体的执行脚本
    '''
    try:
        pconline=onlineinfo()
        return pconline["pc"]
    except Exception:
        return 0
def andiord(name):
    try:
        andonline=onlineinfo()
        return andonline["an"]
    except Exception:
        return 0
def ios(name):
    try:
        iosonline=onlineinfo()
        return iosonline["ios"]
    except Exception:
        return 0
def metric_init(params):
    '''
    这个是初始化的脚本
    '''
    global descriptors
    d1={
        'name': 'pc',   #//名称
        'call_back': pc,  #需要制定的hendler函数
        'time_max': 90,  #监控时间
        'value_type': 'uint',  #类型
        'units': 'C',  #单位
        'slope': 'both',  #获取值
        'format': '%u',  #
        'description': 'Random a number',
        'groups': 'online number'
    }
    d2={
        'name': 'and',   #//名称
        'call_back': andiord,  #需要制定的hendler函数
        'time_max': 90,  #监控事件
        'value_type': 'uint',  #类型
        'units': 'C',  #标签
        'slope': 'both',  #获取值
        'format': '%u',  #
        'description': 'Random a number',
        'groups': 'online number'
    }
    d3={
        'name': 'ios',   #//名称
        'call_back': ios,  #需要制定的hendler函数
        'time_max': 90,  #监控事件
        'value_type': 'uint',  #类型
        'units': 'C',  #
        'slope': 'both',  #获取值
        'format': '%u',  #
        'description': 'Random a number',
        'groups': 'online number'
    }

    descriptors = [d1, d2,d3]
    return descriptors
def metric_cleanup():
    pass
if __name__ == '__main__':
    metric_init({})
    while True:
        for d in descriptors:
            v = d['call_back'](d['name'])
            print ('value for %s is '+d['format']) % (d['name'], v)
        time.sleep(30)
